package com.dm.library.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

public abstract class CommonAdapter<T> extends RecyclerView.Adapter<CommonViewHolder> {

    protected Context mContext;
    private int layoutRes;
    public List<T> beanList;
    private MultiItemTypeSupport typeSupport;
    public OnItemClickListener onItemClickListener;
    public OnItemLongClickListener onItemLongClickListener;

    public CommonAdapter(Context mContext, List<T> beanList, int layoutRes) {
        this.mContext = mContext;
        this.layoutRes = layoutRes;
        this.beanList = beanList;
    }

    public void updateAll(List<T> list) {
        if (null != beanList && null != list) {
            beanList.clear();
            beanList.addAll(list);
        }
    }

    public void add(T item) {
        beanList.add(item);
    }

    public void addAll(List<T> items) {
        beanList.addAll(items);
    }

    public void setTypeSupport(MultiItemTypeSupport typeSupport) {
        this.typeSupport = typeSupport;
    }

    @Override
    public int getItemViewType(int position) {
        if (typeSupport != null && beanList.size() != 0) {
            if (position >= beanList.size()) {
                return typeSupport.getItemType(position, null);
            } else {
                return typeSupport.getItemType(position, beanList.get(position));
            }
        }
        return super.getItemViewType(position);
    }


    @Override
    public CommonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (typeSupport != null) {
            layoutRes = typeSupport.getLayoutId(viewType);
        }
        return CommonViewHolder.createViewHolder(parent, layoutRes);
    }

    @Override
    public void onBindViewHolder(final CommonViewHolder holder, final int position) {
        holder.itemView.setTag(position);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(holder.itemView, position);
                }
            }
        });
        if (beanList != null && beanList.size() != 0) {
            if (position > beanList.size() - 1) {
                convert(holder, null, position);
            } else {
                convert(holder, beanList.get(position), position);
            }
        }
    }

    protected abstract void convert(CommonViewHolder holder, T t, int position);

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    public T getItem(int position) {
        return beanList.get(position);
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int position);
    }

    public interface OnItemLongClickListener {
        boolean onItemLongClick(View v, int position);
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface MultiItemTypeSupport<T> {
        int getLayoutId(int itemType);

        int getItemType(int position, T t);

    }


    public List<T> getItemList() {
        return beanList;
    }

}
